package com.ihr360.payroll.model.dto.salary.profile;


import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.Lists;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.payroll.config.SalaryFieldTypeEntity;
import com.ihr360.payroll.entity.staff.SalaryProfileFieldEntity;
import com.ihr360.payroll.model.dto.salary.basecode.SalaryFieldType;
import com.ihr360.payroll.model.dto.staff.SalaryProfileField;
import com.ihr360.shared.entitywithlistener.NumberIdWithTimeAndCompanyId;
import org.springframework.beans.BeanUtils;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import java.util.List;

/**
 * 公司级别的薪资档案,薪资档案可以作为薪资单的引用项目
 */
@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = {"companyId"}))
public class CompanySalaryProfile extends NumberIdWithTimeAndCompanyId<Long> {


    @OneToMany(cascade = CascadeType.ALL,orphanRemoval = true,fetch = FetchType.EAGER)
    @JoinColumn(nullable = false,name = "companySalaryProfileId")
    private List<SalaryProfileField> profileFields;

    @JsonProperty
    public List<SalaryProfileFieldEntity>  getProfileFieldEntitys() {

        if (CollectionUtils.isEmpty(profileFields)) {
            return Lists.newArrayList();
        }
        List<SalaryProfileFieldEntity> salaryProfileFieldEntities = Lists.newArrayListWithCapacity(profileFields.size());
        profileFields.forEach(profileField -> {
            SalaryProfileFieldEntity salaryProfileFieldEntity = new SalaryProfileFieldEntity();
            BeanUtils.copyProperties(profileField, salaryProfileFieldEntity);
            SalaryFieldType type = profileField.getType();
            if (type != null) {
                SalaryFieldTypeEntity typeEntity = new SalaryFieldTypeEntity();
                BeanUtils.copyProperties(type, typeEntity);
                salaryProfileFieldEntity.setType(typeEntity);
            }
            salaryProfileFieldEntities.add(salaryProfileFieldEntity);
        });
        return salaryProfileFieldEntities;

    }

    public List<SalaryProfileField> getProfileFields() {
        return profileFields;
    }

    public void setProfileFields(List<SalaryProfileField> profileFields) {
        this.profileFields = profileFields;
    }
}